<template>
  <a-modal
    :title="modalTitle"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="业务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="false">
          <a-input v-if="modalOpera==1" v-decorator="['id', {rules: [{required: true}]}]" />
        </a-form-item>

        <a-form-item label="模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', {rules: [{required: true, min: 3, message: '请输入模板名称！'}]}]" />
        </a-form-item>

        <a-form-item label="模板编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input :disabled="modalOpera==1" placeholder="模板编码必须为字母" v-decorator="['templateCode', {rules: [{required: true, min: 3, message: '请输入模板编码！'}]}]" />
        </a-form-item>
		
        <a-form-item label="是否共享" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch
            checked-children="共享"
            un-checked-children="私有"
            v-decorator="['isShare',{ rules: [{required: true}],initialValue: false,valuePropName: 'checked'}]"
            />
        </a-form-item>
		
        <a-form-item label="模板分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tree-select
            v-decorator="['typeId', {rules: [{required: true, message: '请选择部门'}]}]"
            :treeData="treeData"
            :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
            placeholder="请选择模板分类"
            allowClear
            treeDefaultExpandAll
          ></a-tree-select>
        </a-form-item>

        <a-form-item label="开发者名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['developerName', {rules: [{required: true, min: 1, message: '请输入至少一个字符的规则！'}]}]" />
        </a-form-item>

        <a-form-item label="售价" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number style="width: 100%;" :min="1" :max="5000" v-decorator="['price',{rules: [{required: true, message: '请输入售价'}],initialValue:0}]" />
          <!-- <a-input v-decorator="['price', {rules: [{required: true,  message: '请输入售价！'}]}]" /> -->
        </a-form-item>

        <a-form-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input placeholder="开发者联系方式" v-decorator="['developerContact', {rules: [{required: false, min: 3, message: '请输入至少三个字符的规则！'}]}]" />
        </a-form-item>

        <a-form-item label="模板描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['templateDesc', {rules: [{required: false, message: '请输入模板描述！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    save,
    getById,
    update
  } from '@/api/site/template'
  import { page as loadTempTypeTreeData } from '@/api/site/templateType'
  import pick from 'lodash.pick'
  export default {
    data () {
      return {
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 7
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 13
          }
        },
        modalTitle: '',
        modalOpera: 0, // 0：新增 1：编辑
        visible: false,
        treeData: [],
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    created () {
      this.loadTreeData()
    },
    methods: {
      add () {
        this.modal('新建模板', 0)
      },
      edit (record) {
        this.modal('编辑模板', 1)
        getById({
          id: record.id
        }).then(response => {
          this.form.setFieldsValue(pick(response.res, 'id', 'typeId', 'name', 'templateDesc','isShare', 'templateCode', 'templateFolderPath', 'developerName', 'developerContact'
          ))
        })
      },
      modal (title, opera) {
        this.modalTitle = title
        this.modalOpera = opera
        this.visible = true
        this.form.resetFields()
      },
      loadTreeData () {
        loadTempTypeTreeData().then(response => {
          if (response.code === 0) {
            this.treeData = response.res.children || []
          }
        })
      },
      handleSubmit () {
        const {
          form: {
            validateFields
          }
        } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            if (this.modalOpera === 0) {
              save(values).then(response => {
                this.message(response)
              })
            } else if (this.modalOpera === 1) {
              update(values).then(response => {
                this.message(response)
              })
            }
          }
          this.confirmLoading = false
        })
      },
      message (response) {
        if (response.code === 0) {
          this.$message.info(response.msg)
          this.$emit('ok')
          this.handleCancel()
        } else {
          this.$message.error(response.msg)
        }
      },
      handleCancel () {
        this.visible = false
        this.confirmLoading = false
      }
    }
  }
</script>
